package smartcampus.scjava.controller.second;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import smartcampus.scjava.POJO.DO.Schedule;
import smartcampus.scjava.POJO.DO.Student;
import smartcampus.scjava.POJO.DTO.ScheduleDTO;
import smartcampus.scjava.custom.ResultVO;
import smartcampus.scjava.service.CourseService;
import smartcampus.scjava.service.ScheduleService;

import java.util.Date;
import java.util.List;

@RequestMapping("/schedule")
@RestController
@ApiOperation("课程详情表")
public class ScheduleController {

    @Autowired
    ScheduleService scheduleService;

    @Autowired
    CourseService courseService;

    /***
     * 新增一条课程详情表
     * @param schedule
     * @return
     */
    @PostMapping("/insertSchedule")
    @ApiOperation("新增一条课程详情信息")
    public ResultVO<?> insertSchedule(@RequestBody Schedule schedule) {
        System.out.println(schedule);
        //先检测该课程的课程号是否存在
        Boolean a = selectcourseId(schedule.getCourseid());
        //如果存在则返回添加失败
        if (a) {
            return ResultVO.fail("该课程号已存在,请修改后重新添加");
        } else {
            // 如果不存在则执行添加操作
            Date now1 = new Date();
            schedule.setCreateTime(now1);
            Boolean t = scheduleService.insertSchedule(schedule);
            if (t) {
                return ResultVO.success("添加成功");
            } else {
                return ResultVO.fail("添加失败");
            }
        }
    }

    @GetMapping("/delBycourseid")
    @ApiOperation("根据courseid删除一条信息")
    public ResultVO<?> delByid(int courseid) {
        //先检测学生表中是否已经安排该课程
        Boolean a = courseService.selectcourseid(courseid);
        if (null != a) {
            //有安排则不允许删除
            return ResultVO.fail("该课程已分配给学生，不可删除,请删除学生课程安排后重试");
        } else {
            //根据couresid（课程号）删除一条课程信息
            int b = scheduleService.delBycourseid(courseid);
            if (b > 0) {
                return ResultVO.success("删除成功");
            } else {
                return ResultVO.fail("删除失败");
            }
        }
    }

    @PostMapping("/updateBycourseid")
    @ApiOperation("根据courseid修改课程信息")
    public ResultVO<?> updateById(@RequestBody Schedule schedule) {
        boolean aBoolean= scheduleService.updateBycourseid(schedule);
        if (!aBoolean) {
            return ResultVO.fail("修改失败");
        } else {
            return ResultVO.success("修改成功");
        }
    }

    @GetMapping("/selectAllList")
    @ApiOperation("分页查询所有信息")
    public ResultVO<?> selectAllList(@RequestParam(defaultValue = "1") int pageNum,
                                     @RequestParam(defaultValue = "10") int pageSize
    ) {
        //利用pagehelper插件获取页面信息pagenum=第几页，pagesize=每页大小
        PageHelper.startPage(pageNum, pageSize);
        List<Schedule> list = scheduleService.findAll();
        PageInfo<Schedule> pageInfo = new PageInfo<Schedule>(list);
        return ResultVO.success(pageInfo);
    }

    @ApiOperation("根据课程号courseid查询信息")
    public Boolean selectcourseId(int courseid) {
        QueryWrapper<Schedule> qw = new QueryWrapper<Schedule>();
        qw.eq("courseId", courseid);
        Schedule schedule = scheduleService.getOne(qw);
        if (null != schedule) {
            return true;
        } else {
            return false;
        }
    }


}
